package net.alche.fa.core.doing;
import net.alche.fa.core.model.Logic;
import net.alche.fa.core.model.FaValue;
import net.alche.fa.core.CodeLevel;
import net.alche.fa.core.SageStone;
import net.alche.fa.core.FaException;

// =
@FaAnnoDoing
@SuppressWarnings("all") 
public class GiveDo extends AbstractDoing {

  @Override
  public boolean matched(String what, Logic g) {
    return "=".equals(what) || "+=".equals(what) || "-=".equals(what) || "*=".equals(what) || "/=".equals(what) || "%=".equals(what);
  }

  @Override
  public FaValue doing(Logic g, CodeLevel lvl, SageStone st) throws FaException {
    FaValue val = super.getLogicObjVal(st, lvl, g);
    FaValue fv = super.getLogicDataVal(st, lvl, g);
    if ("=".equals(g.getWhat())) {
      return give(val, fv);
    }
    else {
      String what = g.getWhat().substring(0, 1);
      fv = MathDo.doMath(val, what, fv);
      return give(val, fv);
    }
  }

  public FaValue give(FaValue val, FaValue fv) throws FaException {
    if (val == null || fv == null) {
      throw new FaException(FaException.NULL_ERROR, "give ");
    }
    val.giveValue(fv,this.plat);
    return val;
  }
}